import parentView from '@/components/parent-view'
import pageError from '@/views/error/404'
import { getMenuTree } from '@/api/login'

export const loadView = (view) => { // 路由懒加载
    return (resolve) => require([`@/views${view}`], resolve, () => {
        return pageError
    })
}

function filterAsyncRouter(asyncRouterMap, url = '', view = null) {
    return asyncRouterMap.map(route => {
        let initRoute = {
            path: url + route.url,
            meta: {
                title: route.name,
                icon: route.iconPath,
                menuId: route.menuId,
                keepAlive: true
            },
            component: route.children && route.children.length ? parentView : loadView(view ? view + route.url : route.url)
        }
        route.children && route.children.length && (initRoute.children = filterAsyncRouter(route.children, url + route.url, view ?? route.url))
        return initRoute;
    })
}

// 获取首位路由
function filterFirstRouter(router) {
    if (router.children && router.children.length > 0) return filterFirstRouter(router.children[0]);
    else return router
}

function filterButtons(list) {
    let arr = [];
    list.reduce((arr, item) => {
        arr.push(item.buttonKey)
        return arr;
    }, arr)
    return arr;
}

export default {
    namespaced: true,
    state: {
        firstRoutes: '',
        addRoutes: [],
        buttonTomenu: {},
        pageButton: []
    },
    getters: {
        addRoutesEmpty: state => {
            return state.addRoutes.length <= 0
        },
        getMenuList: state => menuId => {
            return state.buttonTomenu[menuId] || []
        }
    },
    mutations: {
        SET_FIRST_ROUTES(state, routes) {
            state.firstRoutes = routes
        },
        SET_ROUTES(state, routes) {
            state.addRoutes = routes
        },
        SET_BUTTONS(state, arr) {
            state.buttonTomenu[arr[0]] = arr[1]
        },
        SET_PAGE_BUTTONS(state, buttons) {
            state.pageButton = buttons;
        }
    },
    actions: {
        generateRouter({ commit }) {
            return new Promise(resolve => {
                getMenuTree().then(res => {
                    if (res) {
                        const rewriteRoutes = [
                            {
                                path: '/home',
                                meta: { title: '首页', show: false },
                                component: () => import('@/views/home')
                            },
                            ...filterAsyncRouter(res.data)
                        ]
                        const initView = [
                            {
                                path: '/',
                                component: () => import('@/views/index'),
                                children: rewriteRoutes
                            }
                        ];
                        commit('SET_FIRST_ROUTES', filterFirstRouter(rewriteRoutes[0]).path)
                        commit('SET_ROUTES', rewriteRoutes)
                        resolve(initView)
                    } else return false;
                })
            })
        },
        getMenuButtons({ commit }, menuId) {
            return new Promise(resolve => {
                res.result.length && commit('SET_BUTTONS', [menuId['menuId'], filterButtons([])])
                commit('SET_PAGE_BUTTONS', filterButtons([]))
                resolve()
            })
        },
        setPageBtn({ commit }, buttons) {
            commit('SET_PAGE_BUTTONS', buttons)
        }
    }
}